

<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%@ include file="/WEB-INF/init.jsp" %>
 
<%
PortletURL portletURL = renderResponse.createRenderURL();

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, 20, portletURL, null, null);

int total = MBMessageCodeLocalServiceUtil.getMBMessageCodesCount();

searchContainer.setTotal(total);
List<MBMessageCode> results = new ArrayList<MBMessageCode>();

for(MBMessageCode cf : allCodeSegments){
	if(cf.getMessageId() > 0){
		results.add(cf);
	}
}

searchContainer.setResults(results);

out.println("Amount: " + results.size());
List resultRows = searchContainer.getResultRows();

for(int i = 0;i < results.size(); i++){
	MBMessageCode messageCode = results.get(i);
	ResultRow row = new ResultRow(messageCode, messageCode.getMessageCodeId(), i);
	
	row.addJSP("/WEB-INF/code.jsp", application, request, response);
	resultRows.add(row);
	
}

%>

<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" paginate="<%=false %>"/>


<%
Set<Long> categorySubscriptionClassPKs = null;
Set<Long> threadSubscriptionClassPKs = null; 

if (themeDisplay.isSignedIn()) {

	List<Subscription> categorySubscriptions = SubscriptionLocalServiceUtil.getUserSubscriptions(user.getUserId(), MBCategory.class.getName());
	categorySubscriptionClassPKs = new HashSet<Long>(categorySubscriptions.size());

	for (Subscription subscription : categorySubscriptions) {
		categorySubscriptionClassPKs.add(subscription.getClassPK());
	}

	threadSubscriptionClassPKs = new HashSet<Long>();

	List<Subscription> threadSubscriptions = SubscriptionLocalServiceUtil.getUserSubscriptions(user.getUserId(), MBThread.class.getName());

	threadSubscriptionClassPKs = new HashSet<Long>(threadSubscriptions.size());

	for (Subscription subscription : threadSubscriptions) {
		threadSubscriptionClassPKs.add(subscription.getClassPK());
	}
}
%>
<liferay-ui:panel-container  extended="<%= false %>" persistState="<%= true %>">
		<liferay-ui:panel collapsible="<%= true %>" cssClass="threads-panel" extended="<%= false %>" id="messageBoardsThreadsPanel" persistState="<%= true %>" title="Siste innlegg">
			<liferay-ui:search-container
							curParam="cur2"
							emptyResultsMessage="there-are-no-threads-in-this-category"
							headerNames="thread,flag,started-by,posts,views,last-post,Stemmer"
							iteratorURL="<%=portletURL %>"
						>
			<%
				List<MBMessage> messages = MBMessageLocalServiceUtil.getGroupMessages(10157, socialEquityUser.getUserId(), WorkflowConstants.STATUS_APPROVED, 0, 10);
			%> 
			
			<liferay-ui:search-container-results
				results = "<%= messages %>"
				total = "<%= messages.size() %>"
			/>
			
			<liferay-ui:search-container-row
				className="com.liferay.portlet.messageboards.model.MBMessage"
				keyProperty="messageId"
				modelVar="message"
			>
			
			<%
				MBThread thread = MBThreadLocalServiceUtil.getMBThread(message.getThreadId());
				//MBMessage message = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());
				
				//int totalThreadScore = com.liferay.ext.util.ThreadRankingUtil.getThreadTotalScoreByThreadId(thread.getThreadId());
				
				message = message.toEscapedModel(); 
				boolean readThread = MBMessageFlagLocalServiceUtil.hasReadFlag(themeDisplay.getUserId(), thread);
	 
				row.setBold(!readThread);
				row.setObject(new Object[] {message, threadSubscriptionClassPKs});
				row.setRestricted(!MBMessagePermission.contains(permissionChecker, message, ActionKeys.VIEW));			
			
			%>
			
			<liferay-portlet:renderURL varImpl="rowURL">
									<portlet:param name="struts_action" value="/message_boards/view_message" />
									<portlet:param name="messageId" value="<%= String.valueOf(message.getMessageId()) %>" />
			</liferay-portlet:renderURL>
			<liferay-ui:search-container-column-text 
									buffer="buffer"
									href="<%=rowURL %>"
									name="message"
								>
				<%
					buffer.append(message.getSubject());
				%>
			</liferay-ui:search-container-column-text>
			
			<liferay-ui:search-container-column-text
									buffer="buffer"
									href="<%=rowURL%>"
									name="flag"
								>
				<%
					buffer.append("flag");
				%>
								
			</liferay-ui:search-container-column-text>
			
			 <liferay-ui:search-container-column-text
									buffer="buffer"
									href="<%= rowURL %>" 
									name="Stemmer"
								> 
								
								<%
									int messageScore = (int)MBMessageRanking.getMessageScore(message.getMessageId());
									buffer.append(messageScore);
								%>
			</liferay-ui:search-container-column-text>					
				
			</liferay-ui:search-container-row>
			</liferay-ui:search-container>
		</liferay-ui:panel>
</liferay-ui:panel-container>

